package lib.pacificapp.widget;

import java.io.File;

import lib.pacificapp.framework.R;
import lib.pacificapp.widget.cropimg.CropImg;
 

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class ImgSelector extends LinearLayout {
   
	 public interface OnSelectListener{
		  void onSelected(Bitmap imgbitmap);
	 }
	 
	 private OnSelectListener onSelectListener;
	public ImgSelector(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		// TODO Auto-generated constructor stub
	}

	public ImgSelector(Context context) {
		super(context);
		init(context);
		// TODO Auto-generated constructor stub
	}

	CropImg img_crop;
    TextView btn_left;//左边按钮
    TextView btn_right;//右边按钮
	private void init(Context _context) {
		 LayoutInflater.from(getContext()).inflate(R.layout.wdg_imgselector, this);
		 
		img_crop = (CropImg) findViewById(R.id.img_crop);
		
		btn_left=(TextView)findViewById(R.id.btn_photos);
		btn_left.setOnClickListener(clicker);
		btn_left.setTag("pho");
		btn_right=(TextView)findViewById(R.id.btn_cam);
		btn_right.setOnClickListener(clicker);
		btn_right.setTag("cam");
	}

	private OnClickListener clicker = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
           String tag=String.valueOf(arg0.getTag());
			if (tag.equals("pho")) {
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
				intent.setType("image/*");
				_activity.startActivityForResult(intent, PHOTO);
			}
			if (tag.equals("cam")) {
				
				
				fileName = null;
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
				intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
						ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
			    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory(),
						getFileName())));
				 
				_activity.startActivityForResult(intent, CAMERA);
			}
			if (tag.equals("back")) {
				btn_left.setText("从相册获取");
				btn_left.setTag("pho");
				btn_right.setText("拍摄新照片");
				btn_right.setTag("cam");
			}
			if (tag.equals("ok")) {
				 if(onSelectListener!=null)
					 onSelectListener.onSelected(img_crop.getCroppedImage());
			}
		}
	};

	private Activity _activity;
	public void start(Activity activty,OnSelectListener mOnSelectListener) {
		_activity = activty;
		onSelectListener=mOnSelectListener; 
	}
	public void start(Activity activty,OnSelectListener mOnSelectListener, int def_img) {
		_activity = activty;
		onSelectListener=mOnSelectListener;
		img_crop.resource(def_img);
	}

	private final int PHOTO = 0x90001;
	private final int CAMERA = 0x90002;
	String fileName = null;

	public String getFileName() {
		if (fileName == null) {
			fileName = "t_" + System.currentTimeMillis() + ".jpg";
		}
		return fileName;
	}

	public void refreshInResult(int channel, Intent data) {
	 
		 
		 BitmapFactory.Options opts=new BitmapFactory.Options();
		 opts.inTempStorage = new byte[100 * 1024]; 
	 	opts.inPreferredConfig = Bitmap.Config.RGB_565; 
		opts.inPurgeable = true; 
	 	opts.inSampleSize = 4; 
		opts.inInputShareable = true; 
		  
		Bitmap bm = null;
		if (channel == PHOTO&&data!=null) {
			Uri uri = data.getData();
			 try { 
				bm = BitmapFactory.decodeStream(_activity.getContentResolver()
						.openInputStream(uri),null,opts);
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else if (channel == CAMERA&&data!=null) {
			String output = Environment.getExternalStorageDirectory() + "/"
					+ this.getFileName();
		     Uri imageUri = Uri.fromFile(new File(output));
			  try {
				bm = BitmapFactory.decodeStream(_activity.getContentResolver()
						.openInputStream(imageUri),null,opts);
			 } catch (Exception e) {
				e.printStackTrace();
			 }
           }
		if(bm!=null){
			 btn_left.setText("确认");
			btn_left.setTag("ok");
			btn_right.setText("重选");
			btn_right.setTag("back");
			img_crop.setImageBitmap(bm);
		}
	}

}
